﻿
using ReservationModule.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ReservationModule
{
    public partial class Invoice : System.Web.UI.Page
    {
        int resID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Login"] = Session["Login"];
            if (Session["Login"] == "false" || Session["Login"] == null)
            {
                Response.Redirect("Reject.aspx");
            }


            CheckoutPage previousPage = (CheckoutPage)this.PreviousPage;
            if (previousPage != null)
            {
                lblCustomerID.Text = previousPage.CustomerID;
                lblFirstName.Text = previousPage.FirstName;
                lblLastName.Text = previousPage.LastName;
                lblNRIC.Text = previousPage.NRIC;
                lblPaymentMode.Text = previousPage.PaymentMode;
                lblInvoice.Text = previousPage.InvoiceID;
                lblDate.Text = DateTime.Today.ToShortDateString();
                resID = Convert.ToInt32(previousPage.ReservationID);
                lblReservationID.Text = Convert.ToString(resID);
                lblRoomCharges.Text = previousPage.RoomCharges;
                lblTax.Text = previousPage.Tax;
                lblTotal.Text = previousPage.Total;
            }
        }

        protected void btnDone_Click(object sender, EventArgs e)
        {
            string paymentInvoiceID = "INV" + lblInvoice.Text;
            DateTime checkInDate = Convert.ToDateTime(GridView1.Rows[0].Cells[0].Text);
            int customerID = Convert.ToInt32(lblCustomerID.Text);
            int reservationID = Convert.ToInt32(lblReservationID.Text);
            DateTime checkOutDate = Convert.ToDateTime(GridView1.Rows[0].Cells[1].Text);
            int numberOfNights = Convert.ToInt32(GridView1.Rows[0].Cells[4].Text);
            int numberOfRooms = Convert.ToInt32(GridView1.Rows[0].Cells[5].Text);
            int numberOfGuestAdults = Convert.ToInt32(GridView1.Rows[0].Cells[2].Text);
            int numberOfGuestChildren = Convert.ToInt32(GridView1.Rows[0].Cells[3].Text);
            string paymentMode = lblPaymentMode.Text;
            string additionalRemarks = GridView1.Rows[0].Cells[6].Text;
            decimal roomCharges = Convert.ToDecimal(lblRoomCharges.Text);
            decimal tax = Convert.ToDecimal(lblTax.Text);
            decimal total = Convert.ToDecimal(lblTotal.Text);
            int roomID=0;
            string roomType="";
            decimal roomRate = 0;
            string description="";
            if (GridView2.Rows.Count > 1)
            {
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    roomID = Convert.ToInt32(GridView2.Rows[i].Cells[0].Text);
                    roomType = GridView2.Rows[i].Cells[1].Text;
                    roomRate = Convert.ToDecimal(GridView2.Rows[i].Cells[2].Text);
                    description = GridView2.Rows[i].Cells[3].Text;
                    PaymentInvoiceDetail invd = new PaymentInvoiceDetail() { PaymentInvoiceID1 = paymentInvoiceID, RoomID1 = roomID, RoomType1 = roomType, RoomRate1 = roomRate, Description1 = description };
                    int rows2 = DBManager.CreatePaymentInvoiceDetail(invd);
                }
            }
            else
            {
                roomID = Convert.ToInt32(GridView2.Rows[0].Cells[0].Text);
                roomType = GridView2.Rows[0].Cells[1].Text;
                roomRate = Convert.ToDecimal(GridView2.Rows[0].Cells[2].Text);
                description = GridView2.Rows[0].Cells[3].Text;
                PaymentInvoiceDetail invd = new PaymentInvoiceDetail() { PaymentInvoiceID1 = paymentInvoiceID, RoomID1 = roomID, RoomType1 = roomType, RoomRate1 = roomRate, Description1 = description };
                int rows2 = DBManager.CreatePaymentInvoiceDetail(invd);
            }

            PaymentInvoice inv = new PaymentInvoice() { PaymentInvoiceID1 = paymentInvoiceID, CustomerID1 = customerID, ReservationID1 = reservationID, CheckInDate1 = checkInDate, CheckOutDate1 = checkOutDate, NumberOfNights1 = numberOfNights, NumberOfRooms1 = numberOfRooms, NumberOfGuestAdults1 = numberOfGuestAdults, NumberOfGuestChildren1 = numberOfGuestChildren, PaymentMode1 = paymentMode, AdditionalRemarks1 = additionalRemarks, RoomCharges1 = roomCharges, Tax1 = tax, Total1 = total };
            int rows = DBManager.CreatePaymentInvoice(inv);

            int resID = Convert.ToInt32(lblReservationID.Text);
            Room r = new Room() { RoomStatus1 = "Vacant", ReservationID1 = resID };
            int roomRow = DBManager.UpdateRoomStatus(r);

            Response.Redirect("BookingMenu.aspx");
        }
    }
}
    